<template>
  <div class="carg-wrapper1">

    <!--      车辆-->
    <div style="text-align: center;overflow: auto;" :style="{height: (height - 70) + 'px'}" >
      <div :style="{width: 170 + 'px'}" class="item" v-for="(item, index) in carList" :key="index" @click="getCar(item)">
        <div class="licence">{{item.licence}}</div>
        <div class="count">
          <span :style="{color: item.load.length > 0 ? 'white' : 'grey'}">{{item.load.length}}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// 待配送列表
import bus from "@/store/bus.js";
import tool from "@/utils/tool";
export default {
  name: "waitToGo",
  data () {
    return {
      title: '',
      usr: {},
      carList: [],
      unDeliverCargo: [], // 待送货
      width: window.innerWidth,
      height: window.innerHeight,
      cargoModal: false, // 装车明细弹出 框
      cargoModalTitle: '',
      currentCar: {}, // 当前选中车辆
      currentDay: ''
    }
  },
  created() {
    bus.$on('scan-ret', this.scanPs)
  },
  unmounted () {
    bus.$off('scan-ret', this.scanPs)
  },
  mounted () {
    this.currentDay = tool.getDay()
    let deliver = tool.data.getUser()
    if (deliver) {
      this.getCarList()
      this.usr = deliver
    } else {
      this.$router.push('/deliver')
    }
    // bus.$on('scan-ret', this.scanPs)
  },
  methods: {
    search () {
      let billNo = '150601-567574'
      this.scanPs({code: billNo})
    },
    scanPs ({code}) {
      let arr = code.split('-')
      let billId = arr[1]
      let data = {
        id: 0,
        billId: billId
      }
      this.$router.push({name: 'waitToGoDetail', query: {item: JSON.stringify(data)}})
      //
      // tool.post('scan/findCargo', {code: code}).then(ret => {
      //   if (ret.stat && ret.data) {
      //     let data = {
      //       id: ret.data.carsId,
      //       billId: billId
      //     }
      //     this.$router.push({name: 'waitToGoDetail', query: {item: JSON.stringify(data)}})
      //   }
      // })
    },
    getList () {
      this.getCarList()
    },
    getCarList () {
      tool.post('deliver/carList', {}).then(ret => {
        for (let i = 0; i < ret.data.length; i++) {
          ret.data[i].load = []
        }
        this.carList = ret.data
        this.cargoDetail()
      })
    },
    // 待送列表
    cargoDetail () {
      this.unDeliverCargo = []
      tool.post('deliver/unDeliver', {}).then(ret => {
        if(ret.stat) {
          let data = ret.data
          for (let i = 0; i < this.carList.length; i++) {
            for (let j = 0; j < data.length; j++) {
              if (data[j].carsId === this.carList[i].id) {
                this.carList[i].load.push(data[j])
              }
            }
          }
          this.unDeliverCargo = data
        }
      })
    },
    getCar (item) {
      this.$router.push({name: 'waitToGoDetail', query: {item: JSON.stringify(item)}})
    },
  }
}
</script>
<style scoped lang="scss">
$item-color: #66CCCC;
.carg-wrapper1 {
  background-color: #f8f8f9;
  text-align: center;
  .item {
    height: 170px;
    display: inline-block;
    border: 1px solid $item-color;
    margin: 10px;
    border-radius: 4px;
    color: white;
    background-color: $item-color;
    .licence {
      padding: 20px 0;font-size: 16px;font-weight: bold;
    }
    .count {
      font-size: 16px;font-weight: bold;margin-top: 40px;
    }
    &:active {
      border: 1px solid orangered;
      background-color: orangered;
      color: white;
    }
  }
}
</style>
